import {
  flow,
  household,
  reset_params,
  important,
  target_household,
} from "../js/request_params";
import { selectResPopInfo, houseHoldRegistered } from "../api/resident_request";
import { selectFlowInfo } from "@/api/floPop";
import { select_imp_info } from "@/api/impPop";
import { ElMessage } from "element-plus";
export const select_idnum_info = async (value, path) => {
  console.log(path);
  let response = await selectResPopInfo({ idNum: value });
  if (response.result == null) {
    ElMessage.info({
      message: response.msg,
      duration: 3000,
    });
    reset_params(household.value);
    return;
  }
  if (path === "migration_establishment") {
    let keys = Object.keys(response.result);
    keys.forEach((key) => {
      target_household.value[key] = response.result[key];
    });
    return;
  }
  let keys = Object.keys(response.result);
  keys.forEach((key) => {
    if (
      (key.toString() == "housenum" || key.toString() == "relationId") &&
      (path === "family_establishment" || path === "collectively_establishment")
    ) {
      return;
    }
    if(path == "householdDeclaration_establishment" && key.toString() != "housenum" && key.toString() != "address"){
      return;
    }
    household.value[key] = response.result[key];
  });
  //
  if (path === "family_establishment") {
    household.value.housetypeId = 1;
    household.value.changetype = "户口立户";
  } else if (path === "collectively_establishment") {
    household.value.housetypeId = 2;
    household.value.changetype = "户口立户";
  }
  household.value.popTypeId = 1;
};


export const submit_service_form = async (service_form, path) => {
  let response = await houseHoldRegistered(household.value);
  ElMessage.success({
    message: response.msg,
    duration: 3000,
  });
  service_form.validate((valid) => {});
  setTimeout(()=>{
    location.reload()
  },500)
  
};


/**
 * 根据身份证号检索常住人口信息
 * @param {*} value 
 */
export async function select_Flow_idnum_info (value){
  
  let response = await selectFlowInfo({'idnum':value});
  flow.value = response.result;
  
}
/**
 * 根据身份证号检索重点人口信息
 * @param {*} value 
 */
export function select_imp_idnum_info(value){
  
  select_imp_info({'idnum':value}).then((res)=>{
      important.value = res.result;
      console.log(res.result);
  })
}
/**
 * 根据身份证号检索常住人口信息
 * @param {*} value 
 */
export function select_res_idnum_info(value){
  selectResPopInfo({'idNum':value}).then((res)=>{
      household.value = res.result;
  })
}


